﻿using MediatR;
using Microsoft.VisualBasic;
using Serilog;
using SheepStorage.Domain.Dto.InputDto;
using SheepStorage.Domain.DTO.Output;
using SheepStorage.Domain.Entity;
using SheepStorage.Repository.Interface;
using SheepStorage.Service.Interface;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Yitter.IdGenerator;

namespace SheepStorage.Service.DataImp
{
    /// <summary>
    /// 供应商管理服务实现
    /// </summary>
    public class ProviderManageService : IProviderManageService
    {
        /// <summary>
        /// 基类
        /// </summary>
        private readonly IBaseRepository<ProviderManage> _providerRepository;
        /// <summary>
        /// 构造函数
        /// </summary>
        /// <param name="providerRepository"></param>
        public ProviderManageService(IBaseRepository<ProviderManage> providerRepository)
        {
            _providerRepository = providerRepository;
        }

        /// <summary>
        /// 供应商列表
        /// </summary>
        /// <returns></returns>
        public async Task<PageOutput<ProviderManage>> GetProviderManage(PageInput input)
        {

            var list = _providerRepository.GetAll().Where(c => c.IsDel == 0).ToList();

            if (!string.IsNullOrEmpty(input.ProviderNumber))
            {
                list = list.Where(c => c.ProviderNumber == input.ProviderNumber).ToList();
            }

            if (!string.IsNullOrEmpty(input.ProviderName))
            {
                list = list.Where(c => c.ProviderName.Contains(input.ProviderName)).ToList();
            }

            if (!string.IsNullOrEmpty(input.ProviderPrincipalPhone))
            {
                list = list.Where(c => c.ProviderPrincipalPhone == input.ProviderPrincipalPhone).ToList();
            }

            if (!string.IsNullOrEmpty(input.ProviderMarket))
            {
                list = list.Where(c => c.ProviderMarket == input.ProviderMarket).ToList();
            }

            if (!string.IsNullOrEmpty(input.ProviderSalesman))
            {
                list = list.Where(c => c.ProviderSalesman == input.ProviderSalesman).ToList();
            }

            if (!string.IsNullOrEmpty(input.ProviderUseState))
            {
                list = list.Where(c => c.ProviderUseState == Convert.ToBoolean(input.ProviderUseState)).ToList();
            }



            var result = new PageOutput<ProviderManage>()
            {
                Total = list.Count(),
                PageSize = input.pageSize
            };

            var entity = list.OrderByDescending(c => c.ProviderId).Skip(input.pageSize * (input.pageIndex - 1)).Take(input.pageSize).ToList();

            result.Data = entity;

            return result;

        }



        /// <summary>
        /// 供应商添加
        /// </summary>
        /// <param name="provider">实体</param>
        /// <returns></returns>
        public async Task<int> AddProviderManage(ProviderManage provider)
        {
            provider.ProviderNumber = YitIdHelper.NextId().ToString();
            provider.IsDel = 0;
            provider.UpdateTime = DateTime.Now;
            var result = _providerRepository.Add(provider);

            return result;
        }

        /// <summary>
        /// 批量删除/逻辑删除
        /// </summary>
        /// <param name="idList"></param>
        /// <returns></returns>
        public async Task<int> DeleteProviderManage(params int[] idList)
        {
            if (idList.Length < 0)
            {
                return 0;
            }
            var result = _providerRepository.GetAll().Where(c => idList.Contains(c.ProviderId)).ToList();

            foreach (var item in result)
            {
                item.IsDel = 1;

                item.UpdateTime = DateTime.Now;
            }

            return _providerRepository.UpdateRange(result);

        }


        /// <summary>
        /// 查询单条数据
        /// </summary>
        /// <param name="providerId"></param>
        /// <returns></returns>
        public async Task<ProviderManage> GetProviderManageInfo(int providerId)
        {
            var list = _providerRepository.GetInfoBy(providerId);
            return list;
        }



    }
}
